package com.hfy.music.mapper;

import com.hfy.model.vo.MusicVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

/**
 * @ author 韩
 * time: 2024/5/30 19:39
 */

@Mapper
public interface RecentMusicMapper {

    @Select("SELECT music.id `id`, music.name `name`, musician.musician_name `musician`, album.create_time `createTime`, album.name `album`, music.duration` duration` " +
            " FROM music_recent " +
            " join music on music_recent.music_id = music.id AND music_recent.account = #{account} " +
            " join musician on musician.id = music.musician_id" +
            " join album on album.id = music.album_id " +
            " ORDER BY music_recent.play_time DESC")
    List<MusicVo> queryRecent(@Param("account") Long account);

    // 调用的存储过程 已被替换
    @Select(value = "{call InsertMusicRecent(#{musicId,jdbcType=INTEGER,mode=IN}, #{account,jdbcType=INTEGER,mode=IN})}")
    @Options(statementType = StatementType.CALLABLE)
    void addRecent(@Param("musicId") int musicId, @Param("account") int account);



    @Delete("DELETE FROM music_recent WHERE account = #{account} AND music_id = #{musicId}")
    void deleteSame(@Param("account") Long account,@Param("musicId") Integer musicId);

    @Delete("DELETE FROM music_recent WHERE account = #{account} ORDER BY `play_time` ASC LIMIT 1;")
    void deleteOldest(@Param("account") Long account);

    @Insert("INSERT INTO music_recent (music_id, account) VALUES (#{musicId}, #{account});")
    void insertNewRecord(@Param("account") Long account,@Param("musicId") Integer musicId);

    @Select("SELECT COUNT(*) FROM `music_recent` WHERE account=#{account}")
    Integer queryRecentNum(@Param("account") Long account);
}
